package cn.xxy.converter;

import cn.xxy.TypeConverter;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
 * @author xxy
 * @date 2024/11/20
 */
public class LocalDateConverter implements TypeConverter {


    @Override
    public boolean supports(Class<?> fieldType) {
        return fieldType.equals(LocalDate.class);
    }

    @Override
    public Object apply(Class<?> fieldType, Object value) {
        if(value instanceof LocalDate) {
            return value;
        }
        else if(value instanceof LocalDateTime) {
            return ((LocalDateTime)value).toLocalDate();
        }
        else if(value instanceof Date) {
            return ((Date)value).toLocalDate();
        }
        else if(value instanceof Timestamp) {
            return ((Timestamp)value).toLocalDateTime().toLocalDate();
        }
        return  null;
//        return switch (value) {
//            case LocalDate localDate -> localDate;
//            case LocalDateTime localDateTime -> localDateTime.toLocalDate();
//            case Date sqlDate -> sqlDate.toLocalDate();
//            case Timestamp timestamp -> timestamp.toLocalDateTime().toLocalDate();
//            default -> null;
//        };
    }
}
